#include <iostream>
#include <math.h>
using namespace std;


int main(){
	int n,sum,ans=0;
	cin>>n;
	sum = n;
	int d[n+1];
	d[n] = 0;
	for(int i=0;i<n;i++){
		scanf("%d",&d[i]);
		if(d[i] == 0)
			sum--;
	}
	while(sum > 0){
		int l=0,r=0,len=0,num=0x7fffffff;
		for(int i=0;i<=n;i++){
			if(d[i] <= 0){
				if(len == 0)
					continue;
				r = i-1;
				l = r-len+1;
				ans += num;
				for(int i=l;i<=r;i++){
					d[i] -= num;
					if(d[i] == 0)
						sum--;
				}
				num = 0x7fffffff;
				len = 0;
				continue;
			}
			num = min(num,d[i]);
			len++;
		}
//		if(r-l < len){
//			r = n-1;
//			l = r-len+1;
//			bnum = num;
//		}
//		cout<<sum<<endl;
//		for(int i=0;i<n;i++){
//			cout<<d[i]<<" ";
//		}
//		cout<<endl<<"r:"<<r<<" l:"<<l<<endl;
//		ans += bnum;
//		for(int i=l;i<=r;i++){
//			d[i] -= bnum;
//			if(d[i] == 0)
//				sum--;
//		}
	}
	cout<<ans;
	return 0;
}
